package respository

import (
	"gitee.com/song-yao-shun/MQH/common"
	"gitee.com/song-yao-shun/MQH/model"
	"gitee.com/song-yao-shun/MQH/vo"
	"gorm.io/gorm"
	"log"
)

//定义接口和定义增删改查的方法
type IDictRepository interface {
	Create(dict model.Dict)
	Update(dict model.Dict)
	SelectById(Id int)
	SelectFullFctById(Id int)
	DeleteById(Id int)
	List(dictList vo.DictList)
}

type DictRepository struct {
	DB *gorm.DB
}

func (d DictRepository) Create(dict model.Dict)  (int, error){

	if err := d.DB.Create(&dict).Error; err != nil {
		log.Printf("新增失败，错误信息为：%v",err)
		return 0, err
	}

	return dict.ID, nil
}

func (d DictRepository) Update(dict model.Dict) {
	panic("implement me")
}

func (d DictRepository) SelectById(Id int) {
	panic("implement me")
}

func (d DictRepository) SelectFullFctById(Id int) {
	panic("implement me")
}

func (d DictRepository) DeleteById(Id int) {
	panic("implement me")
}

func (d DictRepository) List(dictList vo.DictList) (dicts []model.Dict, total int64){
	// 条件查询
	if dictList.Type !=""  {
		d.DB = d.DB.Where("type = ?", dictList.Type)
	}

	//总记录数
	d.DB.Model(&model.Dict{}).Count(&total)

	d.DB.Model(&model.Dict{}).Order("created_at desc").Find(&dicts)

	return
}

func NewDictRepository() DictRepository{
	db :=  common.GetDB()
	//db.AutoMigrate(model.Dict{})

	return DictRepository{DB: db}
}
